package com.eliza.livedata.activity.fg

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.lifecycle.ViewModelProvider
import com.eliza.livedata.R
import com.eliza.livedata.vm.SeekBarProgressVm


/**
 * A simple [Fragment] subclass.
 * Use the [SecondSync.newInstance] factory method to
 * create an instance of this fragment.
 */
class FirstSync : Fragment() {

    lateinit var seekbar: SeekBar

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_sysc_first, container, false)
        seekbar = view.findViewById<SeekBar>(R.id.fg_sysc_first_seekBar)

        activity?.let {
            val vm = ViewModelProvider(
                it,
                ViewModelProvider.AndroidViewModelFactory(it.application)
            ).get(
                SeekBarProgressVm::class.java
            )
            vm.progress.observe(it) { i ->

                seekbar.progress = i
            }
            seekBarListener(vm)
        }
        activity?: Log.e(this::class.java.name,"activity is null")

        return view
    }

    private fun seekBarListener(vm: SeekBarProgressVm) {
        seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                vm.progress.value = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
            }
        })
    }

}